package com.dly.blog.service.impl;

import com.dly.blog.domain.Category;
import com.dly.blog.mapper.CategoryMapper;
import com.dly.blog.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.dly.blog.domain.vo.category.CategoryVO;
import com.dly.blog.domain.ro.category.CategoryPageRO;
import com.dly.blog.domain.ro.category.CategoryAddRO;
import com.dly.blog.domain.ro.category.CategoryEditRO;
import com.dly.blog.domain.vo.PageVO;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.AllArgsConstructor;
import cn.hutool.core.collection.ListUtil;

/**
 * <p>
 * 类别 服务实现类
 * </p>
 *
 * @author DLY
 * @since 2024-01-24 13:55:18
 */
@Service
@AllArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public PageVO<CategoryVO> getCategoryPage(CategoryPageRO categoryPageRO) {
        Page<Category> page = baseMapper.selectPage(categoryPageRO.buildIPage(), new LambdaQueryWrapper<>());
        return PageVO.buildMyBatis(page, category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            return categoryVO;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCategory(CategoryAddRO categoryAddRO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddRO, category);
        baseMapper.insert(category);
    }

    @Override
    public CategoryVO getCategory(String id) {
        Category category = baseMapper.selectById(id);
        CategoryVO categoryVO = new CategoryVO();
        BeanUtils.copyProperties(category, categoryVO);
        return categoryVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCategory(String[] ids) {
        baseMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCategory(CategoryEditRO categoryEditRO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryEditRO, category);
        baseMapper.updateById(category);
    }

}
